//
//  MainTabBarController.m
//  Kjs
//
//  Created by cfq on 16/5/26.
//  Copyright © 2016年 kjs. All rights reserved.
//

#import "MainTabBarController.h"
#import "UIColor+KjsColor.h"
#import <YWFeedbackFMWK/YWFeedbackKit.h>
#import "AppDelegate.h"
#import "RaiseIndexForOrgViewController.h"
#import "RaiseDetailViewController.h"
#import "KJSRequest.h"
@interface MainTabBarController()<UITabBarControllerDelegate>
@property (nonatomic, strong) YWFeedbackKit *feedbackKit;
@end
@implementation MainTabBarController
-(void) viewDidLoad{
    
    self.delegate = self;
    self.viewControllers[0].tabBarItem.tag = 0;
    self.viewControllers[1].tabBarItem.tag = 1;
    self.viewControllers[2].tabBarItem.tag = 2;
    self.viewControllers[3].tabBarItem.tag = 3;
    self.viewControllers[4].tabBarItem.tag = 4;
    CurentDelegate.ifReLoad = YES;
}

-(void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
    NSLog(@"item.tag = %ld",item.tag);
    
    switch (item.tag) {
        case 4:
        {
            [GUATool showDialog:@"功能暂未开启" message:nil waitingTime:1.5];
        }
//            [self _tryLogin];
            break;
        case 2:
        {
            //个人和默认情况下，显示个人融资首页
            NSLog(@"tpye = %@",CurentDelegate.account.type);
            if ([CurentDelegate.account.type isEqualToString:@"p"] || [CurentDelegate.account.type isEqualToString:@"n"] || CurentDelegate.account.type == NULL) {
                NSLog(@"展示个人融资");
                RaiseDetailViewController *raiseDetailVC = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"RaiseIndexViewController"];
                [self.viewControllers[2] setViewControllers:@[raiseDetailVC] animated:NO];
            }else if ([CurentDelegate.account.type isEqualToString:@"o"]){
                //机构
                NSLog(@"展示机构融资");
                RaiseIndexForOrgViewController *raiseDetailForOrgVC = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"RaiseIndexForOrgViewController"];
                [self.viewControllers[2] setViewControllers:@[raiseDetailForOrgVC] animated:NO];
            }
        }
        default:
            break;
    }
}

- (void)_tryLogin
{
   
    NSUserDefaults *userDefaults = [[NSUserDefaults alloc] init];
    NSString *password = [[NSString alloc] init];
    password = [userDefaults objectForKey:@"password"];
    NSLog(@"userId=%@,password=%@",CurentDelegate.account.partyId,password);
    if ((CurentDelegate.account!=NULL)&&(![password isEqualToString:@""])) {
        NSLog(@"用账号密码登录");
        self.feedbackKit = [[YWFeedbackKit alloc] initWithOpenIMUserId:CurentDelegate.account.partyId password:password appKey:CurentDelegate.appKey];
    }else{
        NSLog(@"不用账号密码登录");
        self.feedbackKit = [[YWFeedbackKit alloc] initWithAppKey:CurentDelegate.appKey];
    }
   
    _feedbackKit.customUIPlist = @{//是否开启录音功能，
                                   //如果不设置，会依据是否包含`YWFeedbackAudioExtentionFMWK.framework`来判断是否开启录音功能
                                   //如果设置为false,无论是否包含`YWFeedbackAudioExtentionFMWK.framework`，都会关闭录音功能
                                   //如果设置为true,且包含`YWFeedbackAudioExtentionFMWK.framework`的情况下，会开启录音功能
                                   @"enableAudio":@"true"
                                   };

    _feedbackKit.hideContactInfoView = YES;
    [self _openFeedbackViewController];
}

- (void)_openFeedbackViewController
{    __weak typeof(self) weakSelf = self;

    [_feedbackKit makeFeedbackViewControllerWithCompletionBlock:^(YWFeedbackViewController *viewController, NSError *error) {
        
        if ( viewController != nil ) {
            NSLog(@"到这里了");

            [weakSelf.viewControllers[4] setViewControllers:@[viewController] animated:NO];
            viewController.navigationItem.title = @"客服";
            viewController.navigationItem.leftBarButtonItem = nil;
            __weak typeof(weakSelf.viewControllers[4]) weakNav = weakSelf.viewControllers[4];
            [viewController setOpenURLBlock:^(NSString *aURLString, UIViewController *aParentController) {
                UIViewController *webVC = [[UIViewController alloc] initWithNibName:nil bundle:nil];
                UIWebView *webView = [[UIWebView alloc] initWithFrame:webVC.view.bounds];
                webView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
                
                [webVC.view addSubview:webView];
                [weakNav pushViewController:webVC animated:YES];
                [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:aURLString]]];
            }];
//            NSLog(@"error=%@",error);
//            CurentDelegate.ifReLoad = NO;
        }
    }];
}

- (void)actionQuitFeedback
{
    //取消高度补偿
    CGRect viewFram = self.navigationController.view.frame;
    viewFram.size.height = viewFram.size.height - self.navigationController.navigationBar.frame.size.height;
    [self.navigationController.view setFrame:viewFram];
    [self.navigationController popViewControllerAnimated:YES];
    
}

@end
